<template>
  <div>
    <div class="addcate">
        <span>商品分类列表</span>
        <el-button type="primary" icon="el-icon-plus" size="small" @click="addcategoryFn">添加分类</el-button>
    </div>
    <!-- 编辑的对话框 -->
    <el-dialog title="添加商品分类" :visible.sync="dialogFormVisible" width="500px" @close="dialogCloseFN">
          <el-form :model="cateform" :rules="cateRules" ref="addref">
            <el-form-item label="商品分类名称" :label-width="formLabelWidth" prop="cate_name">
              <el-input v-model="cateform.cate_name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="商品分类别名" :label-width="formLabelWidth" prop="cate_alias">
              <el-input v-model="cateform.cate_alias" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button @click="confimFn" type="primary">确 定</el-button>
          </div>
        </el-dialog>
    <el-table
        :data="currentcategory"
        border
        style="width:100%">
        <el-table-column
        fixed
        type="index"
        label="序号"
        width="50">
        </el-table-column>
        <el-table-column
        fixed
        prop="cate_name"
        label="商品分类名称"
        width="200">
        </el-table-column>
        <el-table-column
        prop="cate_alias"
        label="商品分类别名"
        width="200">
        </el-table-column>
        <el-table-column
            label="操作"
            width="200">
            <template slot-scope="scope">
                <el-button
                size="mini"
                @click="handleEdit(scope.row)">编辑</el-button>
                <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination
      v-if="category.length > 10"
      background
      layout="prev, pager, next"
      :page-size="pageSize"
      :current-page="currentPage"
      :total="category.length"
      @current-change="handleCurrentChange">
    </el-pagination>
  </div>
</template>

<script>
import { getcategoryAPI, delcategoryAPI, updatecategoryAPI, addcategoryAPI } from '@/api'
import { mapMutations } from 'vuex'
export default {
  name: 'admin-category',
  data () {
    return {
      category: [],
      formLabelWidth: '120px',
      cateform: {
        cate_name: '',
        cate_alias: ''
      },
      cateRules: {
        cate_name: [
          { required: true, message: '请输入商品分类名称', trigger: 'blur' },
          { pattern: /^\S{1,6}$/, message: '分类名必须为1-6位的非空字符串', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入商品分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须为1-15位的字母/数字', trigger: 'blur' }
        ]
      },
      dialogFormVisible: false,
      // isEdit 修改true/添加false
      isEdit: false,
      editId: '',
      currentPage: 1, // 当前页码
      pageSize: 10 // 每页显示数量
    }
  },
  computed: {
    totalPages () {
      return Math.ceil(this.category.length / this.pageSize) // 总页数
    },
    currentcategory () {
      const startIndex = (this.currentPage - 1) * this.pageSize
      const endIndex = startIndex + this.pageSize
      return this.category.slice(startIndex, endIndex)
    }
  },
  created () {
    this.getcategoryListFn()
  },
  methods: {
    ...mapMutations(['getCategory']),
    // 重置表单
    dialogCloseFN () {
      this.$refs.addref.resetFields()
    },
    // 获取分类列表
    async getcategoryListFn () {
      const { data } = await getcategoryAPI()
      this.category = data.data
      this.getCategory(data.data)
    },
    // 添加分类
    addcategoryFn () {
      this.isEdit = false
      this.editId = ''
      this.dialogFormVisible = true
    },
    // 修改商品分类 - 将isEdit修改为true （修改） 传入id打开对话框
    handleEdit (row) {
      this.isEdit = true
      this.editId = row.id
      this.dialogFormVisible = true
      // 用户点击修改 - 则将下面的属性赋值为初始值
      this.$nextTick(() => {
        this.cateform.cate_name = row.cate_name
        this.cateform.cate_alias = row.cate_alias
      })
    },
    // 确定添加分类
    confimFn () {
      this.$refs.addref.validate(async valid => {
        if (valid) {
          if (this.isEdit) {
            this.cateform.id = this.editId
            const { data } = await updatecategoryAPI(this.cateform)
            if (data.status !== 0) return this.$message.error(data.message)
            this.$message.success(data.message)
          } else {
            const { data } = await addcategoryAPI(this.cateform)
            if (data.status !== 0) return this.$message.error(data.message)
            this.$message.success(data.message)
          }
          this.getcategoryListFn()
          this.dialogFormVisible = false
        } else {
          return false
        }
      })
    },
    // 删除商品分类
    async handleDelete (row) {
      const { id } = row
      const { data } = await delcategoryAPI(id)
      if (data.status !== 0) return this.$message.error(data.message)
      this.$message.success(data.message)
      this.getcategoryListFn()
    },
    handleCurrentChange (newPage) {
      this.currentPage = newPage
    }
  }
}
</script>

<style scoped>
   .addcate{
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
   }
   .el-input{
    width:300px;
   }
   .el-table {
    border-radius: 8px;
  }
  .el-button {
    border-radius: 8px;
  }
  ::v-deep .el-input__inner{
    height: 2.5em;
    border-radius: 14px;
  }
</style>
